
public class MyLinkList {
    static class ListNode{
        ListNode next;
        int data;
        public ListNode(int val){
            this.data = val;
        }
    }
    public ListNode head;
    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        cur.next = this.head;
        this.head = cur;
    }
    //输入的index不合法
    public boolean indexOutOfBrounds(int index){
        if(index<0){
            throw new IndexOutOfBoundsException("index 不合法！");
        }
        int count = 0;
        ListNode cur = this.head;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        if(index>=count){
            throw new IndexOutOfBoundsException("index 不合法！");
        }else{
            return false;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        ListNode prev = this.head;
        if(this.head==null){
            head = cur;
            return;
        }else{
            while(prev.next!=null){
                prev = prev.next;
            }
            prev.next = cur;
            cur.next = null;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex (int index,int data){
        if(!indexOutOfBrounds(index)){
            ListNode newnode = new ListNode(data);
            if(index==0){
                addFirst(data);
                return ;
            }
            if(index==size()){
                addLast(data);
                return ;
            }
            ListNode cur = this.head;
            while((index-1)>0){
                cur = cur.next;
                index--;
            }
            newnode.next = cur.next;
            cur.next = newnode;
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.data==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head==null){
            System.out.println("链表为空，无法删除");
            return ;
        }
        ListNode cur = this.head;
        if(this.head.data==key){
            this.head = this.head.next;
            return;
        }
        while(cur.next.data!=key){
            cur = cur.next;
            if(cur.next==null){
                System.out.println("没找到");
                return;
            }
        }
        cur.next = cur.next.next;


    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        while(this.head!=null){
            if(this.head.data !=key){
                break;
            }
            this.head = this.head.next;
        }
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.next.data==key){
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }

    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.data+"->");
            cur = cur.next;
        }
        System.out.println("null");
    }
    public void clear(){
        this.head = null;

    }
}
